﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PositionUtils;
using System.Diagnostics;

namespace Tanks
{
    class Barrier : IFieldObject
    {        
        protected int                             health = 0;              //макс здоровье
        protected int                             currentHealth = 0;       //текущее здоровье
        protected Vector2[]                       boards = new Vector2[4]; //границы барьера (4 угла)      
        int timeDead = 0;

        public Barrier(Texture2D _tex,Game1 game, Vector2 position, float rotation, int health)
            : base(game, new PropertiesObject(), _tex)
        {
            if (_tex == Resources.Kirpich)
            {
                Properties.IsKirpich = true;
            }
            else 
            {
                if (_tex == Resources.Beton)
                {
                    Properties.IsBeton = true;
                }
                else
                {
                    if (_tex == Resources.Sand)
                    {
                        Properties.IsSand = true;
                    }
                    else
                    {
                        if (_tex == Resources.Base1)
                        {
                            Properties.IsBase1 = true;
                        }
                        else
                        {
                            if (_tex == Resources.Base2)
                            {
                                Properties.IsBase2 = true;
                            }
                        }
                    }
                }
            }
            Properties.IsFon = false;           
            this.health = health;
            this.currentHealth = health;
            this.position = position;
            this.rotation = rotation;
        }

        public override void Update(GameTime gameTime)
        {            
            Properties.WasActive = false;
           
            //if (!Properties.IsDead)
            //{                
            //}
            //else
            //{
            //    timeDead += gameTime.ElapsedGameTime.Milliseconds;
            //    if (timeDead > 1500) game.DeleteObject(this);
            //}
            
        }
        public override void      Draw()
        {           
            game.SpriteBatch.Draw(texture, position, null, Color.White, rotation,
                    new Vector2(texture.Width / 2, texture.Height / 2), 1.0f, SpriteEffects.None, 0);
            if (!Properties.IsSand && !Properties.IsBeton && !Properties.IsKirpich)
            {
                game.SpriteBatch.Draw(Resources.FullHP, position,
                    new Rectangle(0, 0, Resources.FullHP.Width, (int)(((double)currentHealth / (double)health) * Resources.FullHP.Height)),
                    Color.White, rotation + 3.14f, new Vector2(texture.Width / 2 - 4, Resources.FullHP.Height / 2), 1.0f, SpriteEffects.None, 0);

                game.SpriteBatch.Draw(Resources.LowHP, position,
                    new Rectangle(0, 0, Resources.LowHP.Width, Resources.LowHP.Height - (int)(((double)currentHealth / (double)health) * Resources.LowHP.Height)),
                    Color.White, rotation, new Vector2(-texture.Width / 2 + Resources.LowHP.Width + 4, Resources.LowHP.Height / 2), 1.0f, SpriteEffects.None, 0);
            }
        }
        public override Vector2 getActualPosition()
        {
            return position;
        }
        public override float getActualRotation()
        {
            return rotation;
        }
        public override Vector2[] getActualBoards()
        {
            return mPositionUtils.computeBoards(position, rotation, texture.Height, texture.Width);
        }
        public override Vector2[] getBoards()
        {
            return boards;
        }
        public override void collisionNotFounded()
        {           
        }
        public override void collisionRotNotFounded()
        {            
        }
        private bool checkBoards(Vector2 newPosition, float newRotation)
        {
            boards = mPositionUtils.computeBoards(newPosition, newRotation, texture.Height, texture.Width);
            for (int i = 0; i < boards.Length; i++)
            {                

                if (boards[i].X <= 0 || boards[i].X >= game.width ||
                    boards[i].Y <= 0 || boards[i].Y >= game.heigth)

                    return false;
            }
            return true;
        }       
        public override void collisionFounded(IFieldObject two)
        {
            if (two.Properties.IsShoot)
            {
                if (Properties.IsKirpich)
                {
                    this.currentHealth = currentHealth - 1;
                }
                else
                {
                    if(!Properties.IsSand&& !Properties.IsBeton)
                    {
                        currentHealth -= two.Properties.Damage;
                    }
                }
                if (Properties.IsKirpich && currentHealth == 1)
                {
                    this.texture = Resources.Kirpich2;
                }
                if (currentHealth <= 0)
                {
                    game.DeleteObject(this);
                }
            }            
            
        }
    }
}
